Imports System.Data.SqlClient
Public Class cls_reciboimputacion_

    Implements ICloneable

#Region "Variables ..."
    Private int_idreciboimputacion As Integer = 0
    Private int_idrecibo As Integer = 0
    Private int_idasiento As Integer = 0
    Private dbl_importe As Double = 0

    Private obj_asiento As cls_asiento_

    Private col_Items As Collections.Generic.List(Of cls_reciboimputacion_) = Nothing
#End Region

#Region "Propiedades ..."
    Property idreciboimputacion() As Integer
        Get
            idreciboimputacion = int_idreciboimputacion
        End Get
        Set(ByVal value As Integer)
            int_idreciboimputacion = value
        End Set
    End Property
    Property idrecibo() As Integer
        Get
            idrecibo = int_idrecibo
        End Get
        Set(ByVal value As Integer)
            int_idrecibo = value
        End Set
    End Property
    Property idasiento() As Integer
        Get
            idasiento = int_idasiento
        End Get
        Set(ByVal value As Integer)
            int_idasiento = value
        End Set
    End Property
    Property importe() As Double
        Get
            importe = dbl_importe
        End Get
        Set(ByVal value As Double)
            dbl_importe = value
        End Set
    End Property
    Property asiento() As cls_asiento_
        Get
            If IsNothing(obj_asiento) Then
                obj_asiento = New cls_asiento_
                If Not obj_asiento.Buscar(idasiento) Then
                    obj_asiento = Nothing
                End If
            End If
            asiento = obj_asiento
        End Get
        Set(ByVal value As cls_asiento_)
            obj_asiento = value
        End Set
    End Property
    Property Items() As Collections.Generic.List(Of cls_reciboimputacion_)
        Get
            Items = col_Items
        End Get
        Set(ByVal value As Collections.Generic.List(Of cls_reciboimputacion_))
            col_Items = value
        End Set
    End Property
#End Region

    Private Sub CargarColeccion(ByVal obj_Reader As SqlDataReader)
        col_Items = New Collections.Generic.List(Of cls_reciboimputacion_)
        While obj_Reader.Read()
            Dim Elemento As New cls_reciboimputacion_
            Elemento.int_idreciboimputacion = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idreciboimputacion")))

            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idrecibo"))) = False Then
                Elemento.int_idrecibo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idrecibo")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idasiento"))) = False Then
                Elemento.int_idasiento = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idasiento")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("importe"))) = False Then
                Elemento.dbl_importe = (obj_Reader.GetValue(obj_Reader.GetOrdinal("importe")))
            End If
            col_Items.Add(Elemento)
        End While
    End Sub

    Private Sub CargarVariables(ByVal obj_Reader As SqlDataReader)
        obj_Reader.Read()
        int_idreciboimputacion = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idreciboimputacion")))

        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idrecibo"))) = False Then
            int_idrecibo = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idrecibo")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idasiento"))) = False Then
            int_idasiento = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idasiento")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("importe"))) = False Then
            dbl_importe = (obj_Reader.GetValue(obj_Reader.GetOrdinal("importe")))
        End If
    End Sub

    Public Sub Nuevo(Optional ByVal ObtenerID As Boolean = False)
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "reciboimputacion__Nuevo"
                .Parameters.Add("@idrecibo", SqlDbType.Int).Value = int_idrecibo
                .Parameters.Add("@idasiento", SqlDbType.Int).Value = int_idasiento
                .Parameters.Add("@importe", SqlDbType.Decimal).Value = dbl_importe
                .ExecuteNonQuery()
            End With
            If ObtenerID Then
                CargarUltimo()
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Crear el registro de reciboimputacion_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Modificar()
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "reciboimputacion__Modificar"
                .Parameters.Add("@idreciboimputacion", SqlDbType.Int).Value = int_idreciboimputacion
                .Parameters.Add("@idrecibo", SqlDbType.Int).Value = int_idrecibo
                .Parameters.Add("@idasiento", SqlDbType.Int).Value = int_idasiento
                .Parameters.Add("@importe", SqlDbType.Decimal).Value = dbl_importe
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Modificar el registro de reciboimputacion_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Eliminar(ByVal idreciboimputacion As Integer)
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "reciboimputacion__Eliminar"
                .Parameters.Add("@idreciboimputacion", SqlDbType.Int).Value = idreciboimputacion
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Eliminar el registro de reciboimputacion_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Function Buscar(ByVal idreciboimputacion As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            Buscar = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "reciboimputacion__Buscar"
                .Parameters.Add("@idreciboimputacion", SqlDbType.Int).Value = idreciboimputacion
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                Buscar = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Buscar el registro de reciboimputacion_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Sub CargarUltimo()
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "reciboimputacion__CargarUltimo"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarUltimo registro de reciboimputacion_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Sub

    Public Function CargarTodos() As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarTodos = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "reciboimputacion__CargarTodos"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarTodos = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarTodos los registros de reciboimputacion_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function CargarXidrecibo(ByVal prm_int_idrecibo As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarXidrecibo = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "reciboimputacion__CargarXidrecibo"
                .Parameters.Add("@idrecibo", SqlDbType.Int).Value = prm_int_idrecibo
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarXidrecibo = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarXidrecibo los registros de reciboimputacion_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim obj_reciboimputacion_Clon As New cls_reciboimputacion_
        obj_reciboimputacion_Clon.int_idreciboimputacion = Me.int_idreciboimputacion
        obj_reciboimputacion_Clon.int_idrecibo = Me.int_idrecibo
        obj_reciboimputacion_Clon.int_idasiento = Me.int_idasiento
        obj_reciboimputacion_Clon.dbl_importe = Me.dbl_importe
        obj_reciboimputacion_Clon.col_Items = Me.col_Items
        Return obj_reciboimputacion_Clon
    End Function

End Class
